﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Tron.Helpers;

namespace Tron
{
    public enum StartingPositions
    {
        Random,
        RandomSymmetric
    }
    class RepeatGameTypeSettingsHelper
    {
        Stopwatch stopwatch = null;
        int batchNumber=0;
        int batchIndex=0;
        StreamWriter sw;
        RepeatGameTypeScoreHelper repeatGameTypeScoreHelper;



        public RepeatGameTypeSettingsHelper(RepeatGameTypeScoreHelper helper)
        {
            sw = new StreamWriter("out.txt", true);
            repeatGameTypeScoreHelper = helper;
            Console.WriteLine("constructor");
        }

        public void setOptions(int gameNumber)
        {
            if (stopwatch == null)
            {
                stopwatch = new Stopwatch();
            }
            else
            {
                stopwatch.Stop();
                System.Console.WriteLine("  {0} seconds.", stopwatch.Elapsed.Seconds);
                stopwatch.Reset();
            }
            stopwatch.Start();
            System.Console.WriteLine("Starting game {0}",gameNumber);

            if (batchIndex == RunsPerBatch)
            {
                batchIndex = 0;
                batchNumber++;

                Console.WriteLine(repeatGameTypeScoreHelper.ToString());
                sw.WriteLine(repeatGameTypeScoreHelper.ToStringForFile());
                sw.Flush();

                repeatGameTypeScoreHelper.Reset();
            }

            Dictionary<string, object> batch = GetBatches()[batchNumber];

            if (batchIndex == 0)
            {
                sw.WriteLine("{0}: Starting batch", DateTime.Now);
                foreach (KeyValuePair<string,object> pair in batch) {
                    sw.WriteLine("  {0}={1}", pair.Key, pair.Value.ToString());
                }
                sw.Flush();
            }

            int boardSize = (int)batch["boardSize"]; 
            List<Vector2> playerStartingPositions = new List<Vector2>();

            StartingPositions type = (StartingPositions)batch["startingPositions"];

            int x1=0, x2=0, y1=0, y2=0;
            switch (type)
            {
                case StartingPositions.Random:
                    x1 = RandomAid.NextIntInRange(1,boardSize);
                    y1 = RandomAid.NextIntInRange(1,boardSize);
                    x2 = RandomAid.NextIntInRange(1,boardSize);
                    y2 = RandomAid.NextIntInRange(1,boardSize);
                    while (x1 == x2 && y1 == y2)
                    {
                        x2 = RandomAid.NextIntInRange(1, boardSize);
                        y2 = RandomAid.NextIntInRange(1, boardSize);
                    }
                    break;
                case StartingPositions.RandomSymmetric:
                    x1 = RandomAid.NextIntInRange(1, boardSize);
                    y1 = RandomAid.NextIntInRange(1, boardSize);

                    x2 = boardSize + 1 - x1;
                    y2 = boardSize + 1 - y1;
                    break;

            }
            playerStartingPositions.Add(new Vector2(x1,y1));
            playerStartingPositions.Add(new Vector2(x2,y2));

            System.Console.WriteLine("Starting positions: {0},{1} {2},{3}", x1, y1, x2, y2);
            Constants.SetOptions("", boardSize+2, boardSize+2, playerStartingPositions);

            batchIndex++;
        }

        public static int GetTotalRuns()
        {
            return RunsPerBatch * GetBatches().Count;
        }

        public static int RunsPerBatch = 100;

        public static List<Dictionary<string,object>> GetBatches()
        {
            return new List<Dictionary<string,object>>() {
                //BatchDictionary(8,StartingPositions.Random),
                //BatchDictionary(16,StartingPositions.Random),
                //BatchDictionary(32,StartingPositions.Random),
                BatchDictionary(8,StartingPositions.RandomSymmetric),
                BatchDictionary(16,StartingPositions.RandomSymmetric),
                BatchDictionary(32,StartingPositions.RandomSymmetric),
                
            };
        }

        public static Dictionary<string, object> BatchDictionary(int boardSize, StartingPositions type)
        {
            return new Dictionary<string, object>() {
                {"boardSize",boardSize},
                {"startingPositions",type}
            };
        }

        public static bool ValidateBatch(Dictionary<string, object> batch) {
            if (!batch.ContainsKey("boardSize"))
                return false;
            if (! (batch["staringPositions"] is StartingPositions))
                return false;

            return true;
        }
    }
}